import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router'
import store from './store'
import panelHead from './components/panelHead.vue'

// 刷新后的动态路由添加
const localData = localStorage.getItem('pzadmin')
if(localData){
  store.commit('dynamicMenu',JSON.parse(localData).menu.routerList)
  store.state.menu.routerList.forEach(item=>{
    router.addRoute('main',item)
  })
}

// 路由前置守卫
router.beforeEach((to,form)=>{
  const token = localStorage.getItem('pz_token')
  if(!token && to.path !== '/login'){
    // 如果没有token且访问的不是登录页，则跳转到登录页
    return { path: '/login' }
  }else if(token && to.path === '/login'){
    // 如果有token且访问的是登录页，则跳转到首页
    return { path: '/' }
  }else{
    return true
  }
})

// 如果您正在使用CDN引入，请删除下面一行。
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

// 注册全局组件
app.component('panelHead', panelHead)
// 路由挂载
app.use(router)
// 挂载store
app.use(store)
app.mount('#app')
